package com.csdig.cms.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.csdig.cms.dao.ContentImgDAO;
import com.csdig.cms.model.ContentImg;
import com.csdig.db.DBUtils;
import com.csdig.db.model.Pagination;
import com.csdig.db.model.Sql;

@Repository
public class ContentImgDAOImpl implements ContentImgDAO {

	@Override
	public void add(ContentImg vo) throws Exception {
		Sql sql = new Sql("insert into content_img(content_id,img_url,convert_url)");
		sql.append("values(:content_id,:img_url,:convert_url)");
		sql.set("content_id", vo.getContentId());
		sql.set("img_url", vo.getImgUrl());
		sql.set("convert_url", vo.getConvertUrl());
		DBUtils.exeUpdate(sql);
	}

	@Override
	public void update(ContentImg vo) throws Exception {
		String sql = "update content_img set content_id=?,img_url=?,convert_url=?  where ";
		DBUtils.exeUpdate(sql, new Object[] { vo.getContentId(), vo.getImgUrl(), vo.getConvertUrl(), });
	}

	@Override
	public Pagination<ContentImg> findByCondition(ContentImg vo, int pageNo, int pageSize) throws Exception {
		Sql sql = new Sql("select * from content_img where 1=1 ");
		// /add condtion here
		return DBUtils.queryPagination(sql, null, pageNo, pageSize, ContentImg.class);
	}

	@Override
	public List<ContentImg> findByCondition(ContentImg vo) throws Exception {
		Sql sql = new Sql("select * from content_img where 1=1 ");
		// /add condtion here
		return DBUtils.queryBeanList(sql, ContentImg.class);
	}

	@Override
	public List<ContentImg> listAll() throws Exception {
		return findByCondition(null);
	}

	@Override
	public List<ContentImg> findByCovertUrl(String convertUrl) throws Exception {
		Sql sql = new Sql("select * from content_img where convert_url=:convert_url ");
		sql.set("convert_url", convertUrl);
		return DBUtils.queryBeanList(sql, ContentImg.class);
	}

	@Override
	public List<ContentImg> findByContentId(Integer contentId) throws Exception {
		Sql sql = new Sql("select * from content_img where content_id=:content_id ");
		sql.set("content_id", contentId);
		return DBUtils.queryBeanList(sql, ContentImg.class);
	}

	@Override
	public void del(Integer contentId) throws Exception {
		String sql = "delete from content_img where content_id=?";
		DBUtils.exeUpdate(sql, new Object[] { contentId });
	}

}
